// Copyright (C) 2021, Flickering Inc. All rights reserved.
// Author: haiquanchen (haiquanchen@flickering.ai)

const { ccclass, property, executeInEditMode } = cc._decorator;

@ccclass
@executeInEditMode
export default class NewClass extends cc.Component {
    _wide = 50;
    @property({
        range: [0, 200],
        slide: true,
    })
    public get wide() {
        return this._wide;
    }
    public set wide(v: number) {
        this._wide = v;
        this.init();
    }
    _radius = 50;
    @property({
        range: [0, 200],
        slide: true,
    })
    public get radius() {
        return this._radius;
    }
    public set radius(v: number) {
        this._radius = v;
        this.init();
    }
    _light = 100;
    @property({
        range: [0, 200],
        slide: true,
    })
    public get light() {
        return this._light;
    }
    public set light(v: number) {
        this._light = v;
        this.init();
    }

    @property({ type: [cc.Slider], displayName: '参数调节' })
    sliders: Array<cc.Slider> = [];

    @property(cc.Color)
    color: cc.Color = new cc.Color(0, 110, 100);

    private _material: cc.MaterialVariant;

    onLoad() {
        this.init();
    }

    // onFocusInEditor() {
    //     this.init();
    // }

    init() {
        if (this.node.getComponent(cc.Label)) {
            this._material = this.node.getComponent(cc.Label).getMaterial(0);
        } else if (this.node.getComponent(cc.Sprite)) {
            this._material = this.node.getComponent(cc.Sprite).getMaterial(0);
        }
        this._material.setProperty('wide', this.wide * 0.01);
        this._material.setProperty('radius', this.radius * 0.01);
        this._material.setProperty('light', this.light * 0.01);
        this._material.setProperty('color_r', this.color.r / 255);
        this._material.setProperty('color_g', this.color.g / 255);
        this._material.setProperty('color_b', this.color.b / 255);
    }

    start() {}

    update(dt) {}

    upWide(e, data) {
        this._material.setProperty('wide', this.sliders[0].progress * 2.0);
    }
    upRadius(e, data) {
        this._material.setProperty('radius', this.sliders[1].progress * 2.0);
    }
    upLight(e, data) {
        this._material.setProperty('light', this.sliders[2].progress * 2.0);
    }
}
